package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.pojo.DtsOrder;
import com.qf.service.OrderService;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import com.qf.vo.GoodsRowsVo;
import com.qf.vo.OrderRowsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class OrderServiceImpl implements  OrderService{
    @Autowired
    private DtsOrderMapper dtsOrderMapper;

    @Override
    public Integer orderCount() {
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);

        Integer count = dtsOrderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<DayStatis> orderStatis(int daysAgo) {
        List<DayStatis> list = dtsOrderMapper.statisIncrOrderCnt(daysAgo);
        return list;
    }

    @Override
    public List<CategorySellAmts> categorySellAmt() {
        List<CategorySellAmts> list = dtsOrderMapper.categorySellStatis();
        return list;
    }

    @Override
    public IPage<DtsOrder> findAllByPage(Integer page, Integer limit, Integer orderStatusArray, String sort, String order, Integer userId, String orderSn) {
        Page<DtsOrder> pageParam = new Page<>(page, limit);
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(orderStatusArray)){
            queryWrapper.like("order_status", orderStatusArray);
        }
        if (!StringUtils.isEmpty(userId)){
            queryWrapper.like("user_id", userId);
        }
        if (!StringUtils.isEmpty(orderSn)){
            queryWrapper.like("order_sn", orderSn);
        }
        if ("asc".equalsIgnoreCase(order)){
            queryWrapper.orderByAsc(sort);
        }else {
            queryWrapper.orderByDesc(sort);
        }

        Page<DtsOrder> selectPage = dtsOrderMapper.selectPage(pageParam, queryWrapper);
        return selectPage;
    }

    @Override
    public Integer selectUidByOrderId(Integer orderId) {
        Integer uid = dtsOrderMapper.selectUidByOrderId(orderId);
        return uid;
    }

    @Override
    public DtsOrder findByOrderId(Integer id) {
        DtsOrder order = dtsOrderMapper.selectById(id);
        return order;
    }

    @Override
    public List<OrderRowsVo> findAll() {
        List<OrderRowsVo> orderRowsVoList = dtsOrderMapper.findAll();
        return orderRowsVoList;
    }

    @Override
    public List<GoodsRowsVo> queryAllGoodsStat() {
        List<GoodsRowsVo> goodsStat = dtsOrderMapper.queryAllGoodsStat();
        return goodsStat;
    }

}
